#!/bin/sh

### BEGIN INIT INFO
# Provides:          nfs-kernel-server
# Required-Start:    $remote_fs nfs-common $portmap $time
# Required-Stop:     $remote_fs nfs-common $portmap $time
# Should-Start:      $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Kernel NFS server support
# Description:       NFS is a popular protocol for file sharing across
#                    TCP/IP networks. This service provides NFS server
#                    functionality, which is configured via the
#                    /etc/exports file.
### END INIT INFO
#
# Startup script for nfs-utils
#
# Source function library.
. /etc/init.d/functions
#
# The environment variable NFS_SERVERS may be set in /etc/default/nfsd
# Other control variables may be overridden here too
test -r /etc/default/nfs-utils && . /etc/default/nfs-utils
#
# Location of executables:
test -x "$NFS_MOUNTD" || NFS_MOUNTD=/usr/sbin/rpc.mountd
test -x "$NFS_NFSD" || NFS_NFSD=/usr/sbin/rpc.nfsd
test -x "$NFS_SVCGSSD" || NFS_SVCGSSD=/usr/sbin/rpc.svcgssd
#
# The user mode program must also exist (it just starts the kernel
# threads using the kernel module code).
test -x "$NFS_MOUNTD" || exit 0
test -x "$NFS_NFSD" || exit 0

case "$NEED_SVCGSSD" in
	yes|no)
		;;
	*)
		NEED_SVCGSSD=no
		;;
esac
#
#----------------------------------------------------------------------
# Startup and shutdown functions.
#  Actual startup/shutdown is at the end of this file.
#mountd
start_mountd(){
	echo -n 'starting mountd: '
	start-stop-daemon --start --exec "$NFS_MOUNTD" -- "$@"
	echo done
}
stop_mountd(){
	echo -n 'stopping mountd: '
	start-stop-daemon --stop --quiet --exec "$NFS_MOUNTD"
	echo done
}
#
#svcgssd
start_svcgssd(){
	modprobe -q rpcsec_gss_krb5
	if [ "$NEED_SVCGSSD" = "yes" ]; then
		echo -n "starting svcgssd: "
		start-stop-daemon --start --exec "$NFS_SVCGSSD" -- "$@"
		echo done
	fi
}
stop_svcgssd(){
	if [ "$NEED_SVCGSSD" = "yes" ]; then
		echo -n "stop svcgssd: "
		start-stop-daemon --stop --exec "$NFS_SVCGSSD"
		echo done
	fi
}
#nfsd
start_nfsd(){
        modprobe -q nfsd
	grep -q nfsd /proc/filesystems || {
		echo NFS daemon support not enabled in kernel
		exit 1
        }
	grep -q nfsd /proc/mounts || mount -t nfsd nfsd /proc/fs/nfsd
	grep -q nfsd /proc/mounts || {
		echo nfsd filesystem could not be mounted at /proc/fs/nfsd
		exit 1
        }

	echo -n "starting nfsd: "
	start-stop-daemon --start --exec "$NFS_NFSD" -- "$@"
	echo done
}
stop_nfsd(){
	echo -n 'stopping nfsd: '
	$NFS_NFSD 0
	if pidof nfsd
	then
		echo failed
	else
		echo done
	fi
}

#----------------------------------------------------------------------
#
# supported options:
#  start
#  stop
#  reload: reloads the exports file
#  restart: stops and starts mountd
#FIXME: need to create the /var/lib/nfs/... directories
case "$1" in
  start)
	test -r /etc/exports && exportfs -r
	start_nfsd
	start_svcgssd
	start_mountd
	test -r /etc/exports && exportfs -a;;
  stop)	exportfs -ua
	stop_mountd
	stop_svcgssd
	stop_nfsd;;
  status)
	status /usr/sbin/rpc.mountd
	RETVAL=$?
	status nfsd
	rval=$?
	[ $RETVAL -eq 0 ] && exit $rval
	exit $RETVAL;;
  reload)	test -r /etc/exports && exportfs -r;;
  restart)
	$0 stop
	$0 start;;
  *)	echo "Usage: $0 {start|stop|status|reload|restart}"
	exit 1;;
esac
